package com.big.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.big.app.config.RedisConfig;
import com.big.app.dao.MessagesDao;
import com.big.app.dto.MessagesDto;
import com.big.app.entity.Messages;
import com.big.app.service.intf.MessagesService;
import com.big.app.util.JedisUtils;
import com.big.app.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: zjl
 * @description
 * @date: 2022/4/18 20:55
 */
@Service
public class MessagesServiceImpl implements MessagesService {
    //实例化日志对象
    private Logger logger= LoggerFactory.getLogger(MessagesServiceImpl.class);
    @Resource
    private MessagesDao dao;

    @Override
    //查询所有
    public R all() {
        List<Messages> types;

        //1.验证是否存在缓存
        if(JedisUtils.esists(RedisConfig.Messages_TYPE)){
            //2.存在，直接查询Redis
            types= JSON.parseArray(JedisUtils.getStr(RedisConfig.Messages_TYPE),Messages.class);

        }else{
            //不存在,查询数据库，同时同步更新到缓存中
            //4.查询数据库
            types=dao.selectList(null);
            //5.存储到缓存
            JedisUtils.setStr(RedisConfig.Messages_TYPE, JSON.toJSONString(types));
        }
        return new R(200,"ok",types);
    }
//        查询哪个区的店铺
    @Override
    public R selectEq(Integer flag) {
        if (dao.selectList(new QueryWrapper<Messages>().eq("flag",flag))!=null){
            return new R(200,"ok",dao.selectList(new QueryWrapper<Messages>().eq("flag",flag)));
        }
        return new R(400,"fail",null);
    }
//        低价排序
    @Override
    public R selectByPrice() {
        if (dao.selectList(new QueryWrapper<Messages>().orderByAsc("price"))!=null){
            return new R(200,"ok",dao.selectList(new QueryWrapper<Messages>().orderByAsc("price")));
        }
        return new R(400,"fail",null);
    }

    @Override
    public R selectByStar() {
        if (dao.selectList(new QueryWrapper<Messages>().orderByDesc("star"))!=null){
            return new R(200,"ok",dao.selectList(new QueryWrapper<Messages>().orderByDesc("star")));
        }
        return new R(400,"fail",null);
    }
//    低价,区域筛选
    @Override
    public R selectByPriceDq(Integer flag) {
        if (dao.selectList(new QueryWrapper<Messages>().eq("flag",flag).orderByAsc("price"))!=null){
            return new R(200,"ok",dao.selectList(new QueryWrapper<Messages>().eq("flag",flag).orderByAsc("price")));
        }
        return new R(400,"fail",null);
    }
//   高评,区域筛选
    @Override
    public R selectByStarDq(Integer flag) {
        if (dao.selectList(new QueryWrapper<Messages>().eq("flag",flag).orderByDesc("star"))!=null){
            return new R(200,"ok",dao.selectList(new QueryWrapper<Messages>().eq("flag",flag).orderByDesc("star")));
        }
        return new R(400,"fail",null);
    }

    @Override
    public R queryHotTop() {
        logger.info("访问开始");
        List<MessagesDto> list;
        //1.验证是否存在缓存
        if(JedisUtils.esists(RedisConfig.Messages_HOT)){
            logger.info("访问Redis");
            list= JSON.parseArray(JedisUtils.getStr(RedisConfig.Messages_HOT),MessagesDto.class);
            logger.info("访问Redis-结束");
        }else {
            //魔法值
            //查询数据库
            logger.info("访问Mysql");
            //五条数据
            list=dao.selectHotTop(5);
            logger.info("访问Mysql-结束");
            //更新缓存
            JedisUtils.setStr(RedisConfig.Messages_HOT,JSON.toJSONString(list));
            logger.info("更新Redis");
        }
        logger.info("访问开始-结束");
        return new R(200,"ok",list);
    }

    @Override
    public R search(String search) {
        if (dao.selectList(new QueryWrapper<Messages>().like("name",search))!=null){
            return new R(200,"ok",dao.selectList(new QueryWrapper<Messages>().like("name",search)));
        }
        return new R(400,"fail",null);
    }
}
